package com.fengwk.support.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import com.fengwk.support.exception.BaseException;

public class HttpTestUtils {

	private static final int HTTP_STATUS_404 = 404;
	
	private static final int HTTP_STATUS_200 = 200;
	
	private static final int CONNECT_TIMEOUT = 3000;
	
	private HttpTestUtils() {
		throw new BaseException("can not instance " + getClass());
	}
	
	public static int connectStatus(String addr) throws IOException  {
        URL url = new URL(addr);
        HttpURLConnection oc = (HttpURLConnection) url.openConnection();
        oc.setUseCaches(false);
        oc.setConnectTimeout(CONNECT_TIMEOUT);
        int status = oc.getResponseCode();
        InputStream is = null;
		try {
			is = oc.getInputStream();
		} catch (IOException e) {
			// ignore
		}
        IoUtils.close(is);
        return status;
    }
	
	public static boolean is404(String addr) throws IOException {
		return HTTP_STATUS_404 == connectStatus(addr);
	}
	
	public static boolean is200(String addr) throws IOException {
		return HTTP_STATUS_200 == connectStatus(addr);
	}
	
}
